//
//  ViewController.m
//  WorkGo
//
//  Created by 李树志 on 2017/9/30.
//  Copyright © 2017年 李树志. All rights reserved.
//

#import "ViewController.h"
#import "WGCoreDataManager.h"
#import "WGDay.h"

@interface ViewController ()<NSFetchedResultsControllerDelegate>
{
    NSFetchedResultsController *_fetchRC;
    WGDay *_WGDAY;
}

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *weekLabel;
@property (weak, nonatomic) IBOutlet UILabel *lunarLabel;
@property (weak, nonatomic) IBOutlet UILabel *holidayLabel;
@property (weak, nonatomic) IBOutlet UILabel *workLabel;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)setupUI
{
    
}

- (void)refreshData
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    
    NSManagedObjectContext *context = [WGCoreDataManager sharedManager].managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WGDay" inManagedObjectContext:context];
    fetchRequest.entity = entity;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@", dateString];
    fetchRequest.predicate = predicate;
    
    NSFetchedResultsController *fetchRC = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
    _fetchRC = fetchRC;
    fetchRC.delegate = self;
    NSError *error = nil;
    BOOL success = [fetchRC performFetch:&error];
    if (!success) {
        NSLog(@"%@", error);
        return;
    }
    
    _WGDAY = fetchRC.fetchedObjects.firstObject;
}


#pragma mark - NSFetchedResultsControllerDelegate

//- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
//    NSLog(@"%s", __func__);
//}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    NSLog(@"%s ---> %@ %@ %@", __func__, anObject, indexPath, newIndexPath);
}

//- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
//    NSLog(@"%s", __func__);
//}

//- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
//    NSLog(@"%s ---> %@ %lu", __func__, sectionInfo, (unsigned long)sectionIndex);
//}


@end
